// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-02-13

package crmeb

import (
	"encoding/json"
	"fmt"
)

// CartInfo
// 来自 CRMEB 订单的购物车商品快照, 存储在 eb_store_order_cart_info 表的 cart_info 字段上.
type CartInfo struct {
	Attr    CartInfoAttr    `json:"attrInfo" label:"购物车属性"`
	Product CartInfoProduct `json:"productInfo" label:"商品属性"`

	Quality any `json:"cart_num" label:"数量"`
	Price   any `json:"sum_price" label:"含税单价"`
	Total   any `json:"sum_true_price" label:"含税总价" desc:"含税单价与数量的乘积"`
}

// NewCartInfo
// 创建购物车商品.
//
//	 {
//	    "id":"405459573692956672",
//	    "uid":223,
//	    "type":"0",
//	    "product_id":1230,
//	    "product_attr_unique":"4692aa9a",
//	    "cart_num":2,
//	    "add_time":1737413410,
//	    "is_pay":0,
//	    "is_del":0,
//	    "is_new":0,
//	    "combination_id":0,
//	    "seckill_id":0,
//	    "bargain_id":0,
//	    "advance_id":0,
//	    "status":1,
//	    "productInfo":{
//	        "id":1230,
//	        "mer_id":0,
//	        "image":"https:\/\/chentai-shop.oss-cn-shanghai.aliyuncs.com\/attach\/2024\/12\/67a02202412061527538218.JPG?x-oss-process=image\/resize,h_150,w_150",
//	        "recommend_image":"",
//	        "slider_image":[
//	            "https:\/\/chentai-shop.oss-cn-shanghai.aliyuncs.com\/attach\/2024\/12\/67a02202412061527538218.JPG"
//	        ],
//	        "store_name":"\u53ef\u53e3\u53ef\u4e50\u9ad8\u7f50330ml*24\u7f50",
//	        "store_info":"",
//	        "keyword":"",
//	        "bar_code":"",
//	        "cate_id":"73,90",
//	        "price":"43.99",
//	        "vip_price":"0.00",
//	        "ot_price":"69.00",
//	        "postage":"0.00",
//	        "unit_name":"\u7bb1",
//	        "sort":1,
//	        "sales":58,
//	        "stock":41,
//	        "is_show":1,
//	        "is_hot":0,
//	        "is_benefit":0,
//	        "is_best":0,
//	        "is_new":0,
//	        "is_virtual":0,
//	        "virtual_type":0,
//	        "add_time":1733470447,
//	        "is_postage":0,
//	        "is_del":0,
//	        "mer_use":0,
//	        "give_integral":0,
//	        "cost":"0.00",
//	        "is_seckill":0,
//	        "is_bargain":0,
//	        "is_good":0,
//	        "is_sub":0,
//	        "is_vip":0,
//	        "ficti":0,
//	        "browse":0,
//	        "code_path":"",
//	        "soure_link":"",
//	        "video_link":"",
//	        "temp_id":0,
//	        "spec_type":0,
//	        "activity":"0,1,2,3",
//	        "spu":"1024857508195",
//	        "label_id":"",
//	        "command_word":"",
//	        "recommend_list":"",
//	        "vip_product":0,
//	        "presale":0,
//	        "presale_start_time":0,
//	        "presale_end_time":0,
//	        "presale_day":1,
//	        "logistics":"1",
//	        "freight":2,
//	        "custom_form":"[]",
//	        "is_limit":0,
//	        "limit_type":0,
//	        "limit_num":0,
//	        "min_qty":1,
//	        "express_delivery":true,
//	        "store_mention":false,
//	        "attrInfo":{
//	            "id":14592,
//	            "product_id":1230,
//	            "suk":"\u9ed8\u8ba4",
//	            "stock":41,
//	            "sales":56,
//	            "price":"43.99",
//	            "image":"https:\/\/chentai-shop.oss-cn-shanghai.aliyuncs.com\/attach\/2024\/12\/67a02202412061527538218.JPG?x-oss-process=image\/resize,h_150,w_150",
//	            "unique":"4692aa9a",
//	            "cost":"0.00",
//	            "bar_code":"SP00086",
//	            "ot_price":"69.00",
//	            "vip_price":"0.00",
//	            "weight":"0.00",
//	            "volume":"0.00",
//	            "brokerage":"0.00",
//	            "brokerage_two":"0.00",
//	            "type":0,
//	            "quota":0,
//	            "quota_show":0,
//	            "is_virtual":0,
//	            "coupon_id":0,
//	            "disk_info":""
//	        }
//	    },
//	    "attrInfo":{
//	        "id":14592,
//	        "product_id":1230,
//	        "suk":"\u9ed8\u8ba4",
//	        "stock":41,
//	        "sales":56,
//	        "price":"43.99",
//	        "image":"https:\/\/chentai-shop.oss-cn-shanghai.aliyuncs.com\/attach\/2024\/12\/67a02202412061527538218.JPG",
//	        "unique":"4692aa9a",
//	        "cost":"0.00",
//	        "bar_code":"SP00086",
//	        "ot_price":"69.00",
//	        "vip_price":"0.00",
//	        "weight":"0.00",
//	        "volume":"0.00",
//	        "brokerage":"0.00",
//	        "brokerage_two":"0.00",
//	        "type":0,
//	        "quota":0,
//	        "quota_show":0,
//	        "is_virtual":0,
//	        "coupon_id":0,
//	        "disk_info":""
//	    },
//	    "attrStatus":true,
//	    "vip_truePrice":0,
//	    "costPrice":"0.00",
//	    "trueStock":41,
//	    "truePrice":43.99,
//	    "sum_price":"43.99",
//	    "price_type":"level",
//	    "is_valid":1,
//	    "postage_price":"0.00",
//	    "origin_postage_price":"0.00",
//	    "use_integral":0,
//	    "integral_price":0,
//	    "coupon_price":0,
//	    "sum_true_price":87.98,
//	    "one_brokerage":"0.87",
//	    "two_brokerage":"0.08",
//	    "staff_brokerage":"0.00",
//	    "agent_brokerage":"0.00",
//	    "division_brokerage":"0.00"
//	}
func NewCartInfo(str string) (obj CartInfo, err error) {
	obj = CartInfo{}
	err = json.Unmarshal([]byte(str), &obj)
	return obj, err
}

func (o CartInfo) GenerateCode() (code string) {
	return fmt.Sprintf(`BKTSP%d`, o.Product.Id)
	// if code = o.Attr.Code; code == "" {
	//     if code = o.Product.Code; code == "" {
	//         code = o.Product.Attr.Code
	//     }
	// }
	// return
}

func (o CartInfo) GenerateName() string {
	return o.Product.Name
}
